<template>
      <div class="app-container">
        <!-- 搜索栏 -->
        <el-form @submit.native.prevent :model="queryParams" :inline="true" v-show="showSearch">
            <el-form-item label="用户昵称">
                <el-input @keyup.enter="handleQuery" v-model="queryParams.keyword" style="width: 200px"
                    placeholder="请输入用户昵称" clearable />
            </el-form-item>
            <el-form-item label="状态">
                <el-select v-model="queryParams.isCheck" placeholder="评论状态" clearable style="width: 200px">
                    <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="来源">
                <el-select v-model="queryParams.commentType" placeholder="评论来源" clearable style="width: 200px">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            </el-form-item>
        </el-form>
        <!-- 操作按钮 -->
        <el-row :gutter="10" class="mb15">
            <el-col :span="1.5">
                <el-button type="danger" plain :disabled="commentIdList.length === 0" icon="Delete"
                    @click="handleDelete(undefined)">批量删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain :disabled="commentIdList.length === 0" icon="Check"
                    @click="handleCheck(undefined)">批量通过</el-button>
            </el-col>
            <RightToolBar @changeShow="changeShowSearch" :showSearch="showSearch" @queryTable="getList"></RightToolBar>
        </el-row>
        <!-- 表格展示 -->
        <el-table border :data="commentList" @selection-change="handleSelectionChange" v-loading="loading">
            <!-- 表格列 -->
            <el-table-column type="selection" align="center" width="55" />
            <!-- 头像 -->
            <el-table-column prop="avatar" label="头像" align="center" width="100">
                <template #default="scope">
                    <img :src="scope.row.avatar" width="40" height="40" class="radius" />
                </template>
            </el-table-column>
            <!-- 评论人昵称 -->
            <el-table-column prop="fromNickname" label="评论人" align="center" width="120" />
            <!-- 回复人昵称 -->
            <el-table-column prop="toNickname" label="回复人" align="center" width="120">
                <template #default="scope">
                    <span v-if="scope.row.toNickname">
                        {{ scope.row.toNickname }}
                    </span>
                    <span v-else>无</span>
                </template>
            </el-table-column>
            <!-- 评论文章标题 -->
            <el-table-column prop="articleTitle" label="文章标题" align="center">
                <template #default="scope">
                    <span v-if="scope.row.articleTitle">
                        {{ scope.row.articleTitle }}
                    </span>
                    <span v-else>无</span>
                </template>
            </el-table-column>
            <!-- 评论内容 -->
            <el-table-column prop="commentContent" label="评论内容" align="center">
                <template #default="scope">
                    <span v-html="scope.row.commentContent" class="comment-content" />
                </template>
            </el-table-column>
            <!-- 评论时间 -->
            <el-table-column prop="createTime" label="评论时间" width="130" align="center">
                <template #default="scope">
                    <div class="create-time">
                        <el-icon>
                            <clock />
                        </el-icon>
                        <span style="margin-left: 10px">{{ formattedDate(scope.row.createTime) }}</span>
                    </div>
                </template>
            </el-table-column>
            <!-- 状态 -->
            <el-table-column prop="isCheck" label="状态" width="90" align="center">
                <template #default="scope">
                    <el-tag v-if="scope.row.isCheck == 0" type="warning">审核中</el-tag>
                    <el-tag v-if="scope.row.isCheck == 1" type="success">通过</el-tag>
                </template>
            </el-table-column>
            <!-- 来源 -->
            <el-table-column prop="commentType" label="来源" width="80" align="center">
                <template #default="scope">
                    <el-tag v-if="scope.row.commentType == 1">文章</el-tag>
                    <el-tag v-if="scope.row.commentType == 2" type="warning">友链</el-tag>
                    <el-tag v-if="scope.row.commentType == 3" type="danger">说说</el-tag>
                </template>
            </el-table-column>
            <!-- 列操作 -->
            <el-table-column label="操作" width="160" align="center">
                <template #default="scope">
                    <el-button v-if="scope.row.isCheck == 0" class="el-icon-check"  size="mini"  plain type="success" @click="handleCheck(scope.row.id)">
                        通过
                    </el-button>
                    <el-button type="danger" class="el-icon-delete" size="mini" plain @click="handleDelete(scope.row.id)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination v-if="count > 0" :total="count" v-model:page="queryParams.current" v-model:limit="queryParams.size"
            @pagination="getList" />
    </div>
</template>

<script>
import { formatDate } from "@/utils/date";
import {getCommentList, deleteComment, checkComment} from '@/api/comment'
export default {
  data() {
    return {
    count: 0,
    showSearch: true,
    loading: false,
    queryParams: {
        current: 1,
        size: 10,
    },
    status: [
        {
            value: 0,
            label: "审核中",
        },
        {
            value: 1,
            label: "通过",
        },
    ],
    options: [
        {
            value: 1,
            label: "文章"
        },
        {
            value: 2,
            label: "友链"
        },
        {
            value: 3,
            label: "说说"
        }
    ],
    commentIdList:[],
    commentList: [],
    }
  },
  methods: {
    handleDelete(id){
      if(id == undefined){
        this.$confirm("此操作将永久删除选中评论, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deleteComment(this.commentIdList).then((data)=>{
              if(data.code == 200){
                this.$message({
                  type: "success",
                  message:'删除成功'
                });
                this.getList();
              }
            })
          })
          .catch(() => {
            this.$message.info("已取消删除");
          });
      }
      else{
        this.commentIdList.push(id);
        this.$confirm("此操作将永久删除该评论, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deleteComment(this.commentIdList).then((data)=>{
              if(data.code == 200){
                this.$message({
                  type: "success",
                  message:'删除成功'
                });
                this.getList();
              }
            })
          })
          .catch(() => {
            this.$message.info("已取消删除");
          });
      }
    },
    handleCheck(id){
      if(id == undefined){
      this.$confirm('此操作将通过选中审核, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          checkComment({idList:this.commentIdList, isCheck:1}).then((data)=>{
            if(data.code == 200){
              this.$message({
                type: 'success',
                message: '审核成功!'
              });
              this.getList();
            }
          })
        }).catch((error) => {
          console.log(error)
          this.$message({
            type: 'info',
            message: '已通过'
          });          
        });
      }else{
        this.commentIdList.push(id);
        this.$confirm("此操作将通过该评论, 是否继续?", "提示", {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type:'warning'
        })
          .then(() => {
            checkComment({idList:this.commentIdList, isCheck:1}).then((data)=>{
              if(data.code == 200){
                this.$message({
                  type: "success",
                  message:'审核成功'
                });
                this.getList();
              }
            })
          })
          .catch(() => {
            this.$message({type:'info',message:'已取消审核'});
          });
      }
    },
    formattedDate(date) {
      return formatDate(date);
    },
    handleQuery(){
       this.getList();
    },
    getList(){
      this.loading = true;
      getCommentList(this.queryParams).then((data)=>{
        if(data.code == 200){
          this.commentList = data.data.recordList;
          this.count = data.data.count;
          this.loading = false;
        }
      })
    },
    handleSelectionChange(selectedItems){
      this.commentIdList = selectedItems.map((item) => item.id);
    },
    //改变搜索显示状态
    changeShowSearch(data){
      this.showSearch = data;
    },
  },
  mounted() {
    this.getList();
  },
}
</script>

<style>
.radius{
  border-radius: 30%;
}
.el-button + .el-button {
    margin-right: 10px;
}
</style>